

import HeadPrefab from "../../../component/HeadPrefab";
import ComponentBase from "../../../engine/ComponentBase";
import { EventManager } from "../../../engine/EventManager";
import UIManager from "../../../engine/UIManager";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { EventConfig } from "../../config/EventConfig";
import { ViewConfig } from "../../config/ViewConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubGroup extends ComponentBase {

    private itemNode: cc.Node;
    private itemNodeList: Array<cc.Node> = []

    private dataList: Array<object> = []

    private txtPage: cc.Label;
    private page_size: number = 2;
    private cur_page: number = 1;
    private totalPage: number = 1


    onEnable(): void {
        super.onEnable();
        EventManager.instance.on(EventConfig.Event_Update_Club_Group, this.getClubTableGroup, this);
        this.itemNode = this.findChild(this.node, "scrollView/view/content/itemNode")
        this.itemNode.active = false;
        this.txtPage = this.getChild(this.node, "txtPage", cc.Label);
        this.cur_page = 1

        let btnAddTableGroup: cc.Node = this.findChild(this.node, "btnAddTableGroup")
        UIHelper.onAddEvent(btnAddTableGroup, () => {
            ClubSocketClient.req_club_create_group(UserModel.instance.nowClub.clubid, () => {
                this.getClubTableGroup(true)
            })
        })
        this.getClubTableGroup()
        this.refresh()
        let btnPre: cc.Node = this.findChild(this.node, "btnPre")
        UIHelper.onAddEvent(btnPre, () => {
            if (this.cur_page > 1) {
                this.cur_page--;
                this.refresh()
            }
        })
        let btnNext: cc.Node = this.findChild(this.node, "btnNext")
        UIHelper.onAddEvent(btnNext, () => {
            if (this.cur_page < this.totalPage) {
                this.cur_page++
                this.refresh()
            }
        })
    }

    private getClubTableGroup(showAddView: boolean = false) {
        ClubSocketClient.req_get_club_table_group(UserModel.instance.nowClub.clubid, (data) => {
            if (data) {
                this.dataList = data;
                this.totalPage = this.dataList.length % this.page_size == 0 ? this.dataList.length / this.page_size : Math.floor(this.dataList.length / this.page_size) + 1;
                if (this.totalPage <= 0) {
                    this.totalPage = 1;
                }
                if (showAddView) {
                    UIManager.instance.showView(ViewConfig.viewConfig.ViewClubAddGroupMember, null, this.dataList[this.dataList.length - 1]["id"])
                }

            } else {
                this.dataList = []
            }
            this.refresh()
        })
    }

    private refresh() {
        this.txtPage.string = this.cur_page + "/" + this.totalPage
        this.unscheduleAllCallbacks()
        this.hideAllChildren(this.itemNode.parent)
        for (let i = 0; i < this.page_size; i++) {
            let realIndex: number = (this.cur_page - 1) * this.page_size + i
            let dataObj: object = this.dataList[realIndex];
            if(dataObj){
                let item: cc.Node = this.itemNode.parent.children[i]
                if (!item) {
                    // this.scheduleOnce(() => {
                        this.initOne(dataObj, i, realIndex)
                    // }, 0.02 * i)
                } else {
                    item.active = true;
                    this.initOne(dataObj, i, realIndex)
                }
            }
        }
    }

    private initOne(dataObj: object, i: number, realIndex:number) {
        let itemNode: cc.Node = this.itemNode.parent.children[i]
        if (!itemNode) {
            itemNode = cc.instantiate(this.itemNode)
            itemNode.parent = this.itemNode.parent;
        }
        itemNode.active = true;
        let userContainer: cc.Node = this.getChild(itemNode, "userScrollView")
        for (let j = 0; j < userContainer.childrenCount; j++) {
            let userItemNode: cc.Node = userContainer.children[j]
            userItemNode.active = false;
        }
        let id: number = dataObj["id"]
        let resultUsers: Array<object> = dataObj["resultUsers"]
        for (let j = 0; j < resultUsers.length; j++) {
            let userItemNode: cc.Node = userContainer.children[j]
            if (!userItemNode) {
                userItemNode = cc.instantiate(userContainer.children[0])
                userItemNode.parent = userContainer
            }
            userItemNode.active = true
            let head: HeadPrefab = this.getChild(userItemNode, "head", HeadPrefab)
            head.setHead(resultUsers[j]["headimg"], resultUsers[j]["lv"])
            let txtName: cc.Label = this.getChild(userItemNode, "txtName", cc.Label)
            txtName.string = resultUsers[j]["nick_name"]
            let txtId: cc.Label = this.getChild(userItemNode, "txtId", cc.Label)
            txtId.string = "ID:" + resultUsers[j]["userid"]
            let btnDelitem: cc.Node = this.findChild(userItemNode, "btnDelitem")
            UIHelper.onAddEvent(btnDelitem, () => {
                ClubSocketClient.req_del_user_to_club_group(UserModel.instance.nowClub.clubid, id, resultUsers[j]["userid"], () => {
                    resultUsers.splice(j, 1)
                    this.refresh()
                })
            })

        }
        let btnadd: cc.Node = this.findChild(itemNode, "btnadd")
        UIHelper.onAddEvent(btnadd, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewClubAddGroupMember, null, id)
        })
        let btnDelGroup: cc.Node = this.findChild(itemNode, "btnDelGroup")
        UIHelper.onAddEvent(btnDelGroup, () => {
            ClubSocketClient.req_club_del_group(UserModel.instance.nowClub.clubid, id, () => {
                this.dataList.splice(i, 1)
                this.refresh()
            })
        })
    }

    // private refresh() {
    //     for (let i = 0; i < this.itemNodeList.length; i++) {
    //         this.itemNodeList[i].active = false;
    //     }
    //     // for (let i = 0; i < this.dataList.length; i++) {
    //     for (let i = this.dataList.length - 1; i >= 0; i--) {
    //         let item: cc.Node = this.itemNodeList[i];
    //         if (!item) {
    //             item = cc.instantiate(this.itemNode)
    //             this.itemNodeList[i] = item
    //             item.parent = this.itemNode.parent
    //         }
    //         item.active = true;
    //         let userContainer: cc.Node = this.getChild(item, "userScrollView")
    //         for (let j = 0; j < userContainer.childrenCount; j++) {
    //             let userItemNode: cc.Node = userContainer.children[j]
    //             userItemNode.active = false;
    //         }
    //         let id: number = this.dataList[i]["id"]
    //         let resultUsers: Array<object> = this.dataList[i]["resultUsers"]
    //         for (let j = 0; j < resultUsers.length; j++) {
    //             let userItemNode: cc.Node = userContainer.children[j]
    //             if (!userItemNode) {
    //                 userItemNode = cc.instantiate(userContainer.children[0])
    //                 userItemNode.parent = userContainer
    //             }
    //             userItemNode.active = true
    //             let head: HeadPrefab = this.getChild(userItemNode, "head", HeadPrefab)
    //             head.setHead(resultUsers[j]["headimg"], resultUsers[j]["lv"])
    //             let txtName: cc.Label = this.getChild(userItemNode, "txtName", cc.Label)
    //             txtName.string = resultUsers[j]["nick_name"]
    //             let txtId:cc.Label = this.getChild(userItemNode, "txtId", cc.Label)
    //             txtId.string = "ID:"+resultUsers[j]["userid"]
    //             let btnDelitem: cc.Node = this.findChild(userItemNode, "btnDelitem")
    //             UIHelper.onAddEvent(btnDelitem, () => {
    //                 ClubSocketClient.req_del_user_to_club_group(id, resultUsers[j]["userid"], () => {
    //                     resultUsers.splice(j, 1)
    //                     this.refresh()
    //                 })
    //             })

    //         }
    //         let btnadd: cc.Node = this.findChild(item, "btnadd")
    //         UIHelper.onAddEvent(btnadd, () => {
    //             UIManager.instance.showView(ViewConfig.viewConfig.ViewClubAddGroupMember, null, id)
    //         })
    //         let btnDelGroup: cc.Node = this.findChild(item, "btnDelGroup")
    //         UIHelper.onAddEvent(btnDelGroup, () => {
    //             ClubSocketClient.req_club_del_group(UserModel.instance.nowClub.clubid, id, () => {
    //                 this.dataList.splice(i, 1)
    //                 this.refresh()
    //             })
    //         })
    //     }
    // }

    onDisable(): void {
        super.onDisable();
    }
}
